package com.ozo.cloud.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ozo.cloud.common.base.constant.ConfigTypeEnum;
import com.ozo.cloud.common.base.model.BaseBatchDTO;
import com.ozo.cloud.common.base.model.BasePage;
import com.ozo.cloud.common.base.model.PageResult;
import com.ozo.cloud.common.core.exception.ServiceException;
import com.ozo.cloud.common.database.util.SortUtils;
import com.ozo.cloud.system.entity.SysConfig;
import com.ozo.cloud.system.exception.SystemExceptionEnum;
import com.ozo.cloud.system.mapper.SysConfigMapper;
import com.ozo.cloud.system.pojo.dto.SysConfigQueryDTO;
import com.ozo.cloud.system.pojo.dto.SysConfigSaveDTO;
import com.ozo.cloud.system.pojo.vo.SysConfigVO;
import com.ozo.cloud.system.service.ISysConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 系统配置 服务实现类
 *
 * @author qiangesoft
 * @version 1.0
 * @date 2024-11-19 20:55:22
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SysConfigSaveDTO saveDTO) {
        // 参数校验
        this.checkParam(null, saveDTO.getGroupCode(), saveDTO.getConfigKey());

        SysConfig sysConfig = BeanUtil.copyProperties(saveDTO, SysConfig.class);
        sysConfig.setConfigType(ConfigTypeEnum.SYSTEM.getCode());
        baseMapper.insert(sysConfig);
    }

    /**
     * 合法性校验
     *
     * @param id
     * @param groupCode
     * @param configKey
     */
    private void checkParam(Long id, String groupCode, String configKey) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConfig::getGroupCode, groupCode)
                .eq(SysConfig::getConfigKey, configKey)
                .ne(Objects.nonNull(id), SysConfig::getId, id);
        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException(SystemExceptionEnum.CONFIG_KEY_EXISTS);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void remove(BaseBatchDTO<Long> batchDTO) {
        baseMapper.deleteBatchIds(batchDTO.getIdList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SysConfigSaveDTO saveDTO) {
        // 参数校验
        this.checkParam(saveDTO.getId(), saveDTO.getGroupCode(), saveDTO.getConfigKey());

        SysConfig sysConfig = BeanUtil.copyProperties(saveDTO, SysConfig.class);
        baseMapper.updateById(sysConfig);
    }

    @Override
    public PageResult<SysConfigVO> page(BasePage basePage, SysConfigQueryDTO queryDTO) {
        Integer pageNum = basePage.getPageNum();
        Integer pageSize = basePage.getPageSize();

        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        // 排序
        String order = basePage.getOrder();
        if (StrUtil.isBlank(order)) {
            queryWrapper.lambda().orderByDesc(SysConfig::getId);
        } else {
            SortUtils.setSortCondition(queryWrapper, basePage.getProp(), order);
        }
        // 条件
        queryWrapper.lambda()
                .like(StrUtil.isNotBlank(queryDTO.getGroupCode()), SysConfig::getGroupCode, queryDTO.getGroupCode())
                .like(StrUtil.isNotBlank(queryDTO.getConfigName()), SysConfig::getConfigName, queryDTO.getConfigName());
        Page<SysConfig> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);

        PageResult<SysConfigVO> pageResult = new PageResult<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setPage(page.getPages());
        pageResult.setTotal(page.getTotal());
        List<SysConfig> recordList = page.getRecords();
        List<SysConfigVO> recordVOList = BeanUtil.copyToList(recordList, SysConfigVO.class);
        pageResult.setResults(recordVOList);
        return pageResult;
    }

}
